//
//  Labs_SwiftCharts_TableVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2024/3/25.
//

import Foundation

enum Example {
    case helloWorld, bars, stackedBars, barsPlusMinus, groupedBars, barsStackedGrouped, scatter, areas, rangedAxis, bubble, coords, target, multival, notifications, combination, equalSpacing, tracker, multiTracker, multiAxis, multiAxisInteractive, candleStick, cubiclines, cubiclinesWithGradient, notNumeric, candleStickInteractive, customUnits, trendline
}

class Labs_SwiftCharts_TableVC: UIViewController {
    
    lazy var tableView: UITableView = {
        let tableV = UITableView(frame: view.bounds, style: .plain)
        tableV.delegate = self
        tableV.dataSource = self
        tableV.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        return tableV
    }()
    
    var examples: [(Example, String)] = [
        (.helloWorld, "Hello World"),
        (.bars, "Bars"),
        (.stackedBars, "Stacked bars"),
        (.barsPlusMinus, "+/- bars with dynamic gradient"),
        (.groupedBars, "Grouped bars"),
        (.barsStackedGrouped, "Stacked, grouped bars"),
        (.combination, "+/- bars and line"),
        (.scatter, "Scatter"),
        (.notifications, "Notifications (interactive)"),
        (.target, "Target point animation"),
        (.areas, "Areas, lines, circles (interactive)"),
        (.rangedAxis, "Ranged axis, rotation"),
        (.bubble, "Bubble, gradient bar mapping"),
        (.notNumeric, "Not numeric values"),
        (.coords, "Show touch coords (interactive)"),
        (.tracker, "Track touch (interactive)"),
        (.multiTracker, "Multi-chart touch tracking"),
        (.equalSpacing, "Fixed axis spacing"),
        (.customUnits, "Custom units, scrollable"),
        (.multival, "Multiple axis labels"),
        (.multiAxis, "Multiple axes"),
        (.multiAxisInteractive, "Multiple axes (interactive)"),
        (.candleStick, "Candlestick"),
        (.candleStickInteractive, "Candlestick (tracker, custom views)"),
        (.cubiclines, "Cubic lines"),
        (.cubiclinesWithGradient, "Cubic lines with gradient"),
        (.trendline, "Trendline")
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        title = "chart系列"
        view.addSubview(tableView)
    }
}

extension Labs_SwiftCharts_TableVC: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return examples.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = examples[indexPath.row].1
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 60
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let example = examples[indexPath.row]
        let controller = DetailViewController()
        controller.detailItem = example.0
        controller.title = example.1
        navigationController?.pushViewController(controller, animated: true)
    }
}
